package com.atguigu.srb.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.atguigu.srb.base.exception.SrbException;
import com.atguigu.srb.base.result.ResponseEnum;
import com.atguigu.srb.oss.properties.OSSProperties;
import com.atguigu.srb.oss.service.OssService;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {
    @Override
    public String upload(MultipartFile file, String module) {
        //获取上传的文件名称
        String fileName= file.getOriginalFilename();
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        //若上传的文件同名 默认会覆盖之前的文件
//        String objectName = "avatar/ff.jpg";
        String objectName = module+new DateTime().toString("/yyyy/MM/dd/")
                + System.currentTimeMillis()+"_"+
                UUID.randomUUID().toString().substring(0,6)+
                fileName.substring(fileName.lastIndexOf("."));//获取文件的扩展名

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OSSProperties.END_POINT, OSSProperties.ACCESS_KEY_ID,OSSProperties.ACCESS_KEY_SECRET);

        try {
            //获取上传文件的输入流
            InputStream inputStream = file.getInputStream();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(OSSProperties.BUCKET_NAME, objectName, inputStream);
            //设置该属性可以返回response 若不设置 则返回的response为空
            putObjectRequest.setProcess("true");
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            //若上传成功 则返回200
//            System.out.println(result.getResponse());
            //拼接上传成功的文件路径
            String imgPath = "https://"+OSSProperties.BUCKET_NAME+OSSProperties.DOMAIN_NAME+objectName;
            System.out.println(imgPath);
            return imgPath;
        } catch (Exception e) {
            System.out.println("Error Message:" + e.getMessage());
            throw new SrbException(ResponseEnum.UPLOAD_ERROR);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void deleteFile(String path) {
        String objectName = path.substring(("https://"+OSSProperties.BUCKET_NAME+OSSProperties.DOMAIN_NAME+"/").length());
        System.out.println(objectName);

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OSSProperties.END_POINT, OSSProperties.ACCESS_KEY_ID,OSSProperties.ACCESS_KEY_SECRET);

        try {
            // 删除文件或目录。如果要删除目录，目录必须为空。
            ossClient.deleteObject(OSSProperties.BUCKET_NAME, objectName);
        } catch (Exception e) {
            System.out.println("Error Message:" + e.getMessage());
            throw new SrbException(ResponseEnum.DELETE_FILE_ERROR);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
